/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.crossriver.crgis.database.service.persistence;

import com.crossriver.crgis.database.NoSuchOrganisation_InformationException;
import com.crossriver.crgis.database.model.Organisation_Information;
import com.crossriver.crgis.database.model.impl.Organisation_InformationImpl;
import com.crossriver.crgis.database.model.impl.Organisation_InformationModelImpl;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the organisation_ information service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link Organisation_InformationUtil} to access the organisation_ information persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Wole Adetiba
 * @see Organisation_InformationPersistence
 * @see Organisation_InformationUtil
 * @generated
 */
public class Organisation_InformationPersistenceImpl extends BasePersistenceImpl<Organisation_Information>
	implements Organisation_InformationPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = Organisation_InformationImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "findByUserId",
			new String[] {
				String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "countByUserId",
			new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "countAll", new String[0]);

	/**
	 * Caches the organisation_ information in the entity cache if it is enabled.
	 *
	 * @param organisation_Information the organisation_ information to cache
	 */
	public void cacheResult(Organisation_Information organisation_Information) {
		EntityCacheUtil.putResult(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationImpl.class,
			organisation_Information.getPrimaryKey(), organisation_Information);
	}

	/**
	 * Caches the organisation_ informations in the entity cache if it is enabled.
	 *
	 * @param organisation_Informations the organisation_ informations to cache
	 */
	public void cacheResult(
		List<Organisation_Information> organisation_Informations) {
		for (Organisation_Information organisation_Information : organisation_Informations) {
			if (EntityCacheUtil.getResult(
						Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
						Organisation_InformationImpl.class,
						organisation_Information.getPrimaryKey(), this) == null) {
				cacheResult(organisation_Information);
			}
		}
	}

	/**
	 * Clears the cache for all organisation_ informations.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(Organisation_InformationImpl.class.getName());
		EntityCacheUtil.clearCache(Organisation_InformationImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the organisation_ information.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(Organisation_Information organisation_Information) {
		EntityCacheUtil.removeResult(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationImpl.class,
			organisation_Information.getPrimaryKey());
	}

	/**
	 * Creates a new organisation_ information with the primary key. Does not add the organisation_ information to the database.
	 *
	 * @param applicationId the primary key for the new organisation_ information
	 * @return the new organisation_ information
	 */
	public Organisation_Information create(String applicationId) {
		Organisation_Information organisation_Information = new Organisation_InformationImpl();

		organisation_Information.setNew(true);
		organisation_Information.setPrimaryKey(applicationId);

		return organisation_Information;
	}

	/**
	 * Removes the organisation_ information with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the organisation_ information to remove
	 * @return the organisation_ information that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a organisation_ information with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove((String)primaryKey);
	}

	/**
	 * Removes the organisation_ information with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param applicationId the primary key of the organisation_ information to remove
	 * @return the organisation_ information that was removed
	 * @throws com.crossriver.crgis.database.NoSuchOrganisation_InformationException if a organisation_ information with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information remove(String applicationId)
		throws NoSuchOrganisation_InformationException, SystemException {
		Session session = null;

		try {
			session = openSession();

			Organisation_Information organisation_Information = (Organisation_Information)session.get(Organisation_InformationImpl.class,
					applicationId);

			if (organisation_Information == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + applicationId);
				}

				throw new NoSuchOrganisation_InformationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					applicationId);
			}

			return remove(organisation_Information);
		}
		catch (NoSuchOrganisation_InformationException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Organisation_Information removeImpl(
		Organisation_Information organisation_Information)
		throws SystemException {
		organisation_Information = toUnwrappedModel(organisation_Information);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, organisation_Information);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationImpl.class,
			organisation_Information.getPrimaryKey());

		return organisation_Information;
	}

	public Organisation_Information updateImpl(
		com.crossriver.crgis.database.model.Organisation_Information organisation_Information,
		boolean merge) throws SystemException {
		organisation_Information = toUnwrappedModel(organisation_Information);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, organisation_Information, merge);

			organisation_Information.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
			Organisation_InformationImpl.class,
			organisation_Information.getPrimaryKey(), organisation_Information);

		return organisation_Information;
	}

	protected Organisation_Information toUnwrappedModel(
		Organisation_Information organisation_Information) {
		if (organisation_Information instanceof Organisation_InformationImpl) {
			return organisation_Information;
		}

		Organisation_InformationImpl organisation_InformationImpl = new Organisation_InformationImpl();

		organisation_InformationImpl.setNew(organisation_Information.isNew());
		organisation_InformationImpl.setPrimaryKey(organisation_Information.getPrimaryKey());

		organisation_InformationImpl.setApplicationId(organisation_Information.getApplicationId());
		organisation_InformationImpl.setUserId(organisation_Information.getUserId());
		organisation_InformationImpl.setOrganisationName(organisation_Information.getOrganisationName());
		organisation_InformationImpl.setNatureOfBusiness(organisation_Information.getNatureOfBusiness());
		organisation_InformationImpl.setRegNo(organisation_Information.getRegNo());
		organisation_InformationImpl.setCountryOfReg(organisation_Information.getCountryOfReg());
		organisation_InformationImpl.setDateOfRegistration(organisation_Information.getDateOfRegistration());

		return organisation_InformationImpl;
	}

	/**
	 * Finds the organisation_ information with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the organisation_ information to find
	 * @return the organisation_ information
	 * @throws com.liferay.portal.NoSuchModelException if a organisation_ information with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey((String)primaryKey);
	}

	/**
	 * Finds the organisation_ information with the primary key or throws a {@link com.crossriver.crgis.database.NoSuchOrganisation_InformationException} if it could not be found.
	 *
	 * @param applicationId the primary key of the organisation_ information to find
	 * @return the organisation_ information
	 * @throws com.crossriver.crgis.database.NoSuchOrganisation_InformationException if a organisation_ information with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information findByPrimaryKey(String applicationId)
		throws NoSuchOrganisation_InformationException, SystemException {
		Organisation_Information organisation_Information = fetchByPrimaryKey(applicationId);

		if (organisation_Information == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + applicationId);
			}

			throw new NoSuchOrganisation_InformationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				applicationId);
		}

		return organisation_Information;
	}

	/**
	 * Finds the organisation_ information with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the organisation_ information to find
	 * @return the organisation_ information, or <code>null</code> if a organisation_ information with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey((String)primaryKey);
	}

	/**
	 * Finds the organisation_ information with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param applicationId the primary key of the organisation_ information to find
	 * @return the organisation_ information, or <code>null</code> if a organisation_ information with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information fetchByPrimaryKey(String applicationId)
		throws SystemException {
		Organisation_Information organisation_Information = (Organisation_Information)EntityCacheUtil.getResult(Organisation_InformationModelImpl.ENTITY_CACHE_ENABLED,
				Organisation_InformationImpl.class, applicationId, this);

		if (organisation_Information == null) {
			Session session = null;

			try {
				session = openSession();

				organisation_Information = (Organisation_Information)session.get(Organisation_InformationImpl.class,
						applicationId);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (organisation_Information != null) {
					cacheResult(organisation_Information);
				}

				closeSession(session);
			}
		}

		return organisation_Information;
	}

	/**
	 * Finds all the organisation_ informations where userId = &#63;.
	 *
	 * @param userId the user id to search with
	 * @return the matching organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public List<Organisation_Information> findByUserId(String userId)
		throws SystemException {
		return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the organisation_ informations where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param start the lower bound of the range of organisation_ informations to return
	 * @param end the upper bound of the range of organisation_ informations to return (not inclusive)
	 * @return the range of matching organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public List<Organisation_Information> findByUserId(String userId,
		int start, int end) throws SystemException {
		return findByUserId(userId, start, end, null);
	}

	/**
	 * Finds an ordered range of all the organisation_ informations where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param start the lower bound of the range of organisation_ informations to return
	 * @param end the upper bound of the range of organisation_ informations to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of matching organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public List<Organisation_Information> findByUserId(String userId,
		int start, int end, OrderByComparator orderByComparator)
		throws SystemException {
		Object[] finderArgs = new Object[] {
				userId,
				
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<Organisation_Information> list = (List<Organisation_Information>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_ORGANISATION_INFORMATION_WHERE);

			if (userId == null) {
				query.append(_FINDER_COLUMN_USERID_USERID_1);
			}
			else {
				if (userId.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_USERID_USERID_3);
				}
				else {
					query.append(_FINDER_COLUMN_USERID_USERID_2);
				}
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(Organisation_InformationModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (userId != null) {
					qPos.add(userId);
				}

				list = (List<Organisation_Information>)QueryUtil.list(q,
						getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_USERID,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Finds the first organisation_ information in the ordered set where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the first matching organisation_ information
	 * @throws com.crossriver.crgis.database.NoSuchOrganisation_InformationException if a matching organisation_ information could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information findByUserId_First(String userId,
		OrderByComparator orderByComparator)
		throws NoSuchOrganisation_InformationException, SystemException {
		List<Organisation_Information> list = findByUserId(userId, 0, 1,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("userId=");
			msg.append(userId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchOrganisation_InformationException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the last organisation_ information in the ordered set where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the last matching organisation_ information
	 * @throws com.crossriver.crgis.database.NoSuchOrganisation_InformationException if a matching organisation_ information could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information findByUserId_Last(String userId,
		OrderByComparator orderByComparator)
		throws NoSuchOrganisation_InformationException, SystemException {
		int count = countByUserId(userId);

		List<Organisation_Information> list = findByUserId(userId, count - 1,
				count, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("userId=");
			msg.append(userId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchOrganisation_InformationException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the organisation_ informations before and after the current organisation_ information in the ordered set where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param applicationId the primary key of the current organisation_ information
	 * @param userId the user id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the previous, current, and next organisation_ information
	 * @throws com.crossriver.crgis.database.NoSuchOrganisation_InformationException if a organisation_ information with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Organisation_Information[] findByUserId_PrevAndNext(
		String applicationId, String userId, OrderByComparator orderByComparator)
		throws NoSuchOrganisation_InformationException, SystemException {
		Organisation_Information organisation_Information = findByPrimaryKey(applicationId);

		Session session = null;

		try {
			session = openSession();

			Organisation_Information[] array = new Organisation_InformationImpl[3];

			array[0] = getByUserId_PrevAndNext(session,
					organisation_Information, userId, orderByComparator, true);

			array[1] = organisation_Information;

			array[2] = getByUserId_PrevAndNext(session,
					organisation_Information, userId, orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Organisation_Information getByUserId_PrevAndNext(
		Session session, Organisation_Information organisation_Information,
		String userId, OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_ORGANISATION_INFORMATION_WHERE);

		if (userId == null) {
			query.append(_FINDER_COLUMN_USERID_USERID_1);
		}
		else {
			if (userId.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_USERID_USERID_3);
			}
			else {
				query.append(_FINDER_COLUMN_USERID_USERID_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByFields = orderByComparator.getOrderByFields();

			if (orderByFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(Organisation_InformationModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		if (userId != null) {
			qPos.add(userId);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByValues(organisation_Information);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<Organisation_Information> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Finds all the organisation_ informations.
	 *
	 * @return the organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public List<Organisation_Information> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the organisation_ informations.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of organisation_ informations to return
	 * @param end the upper bound of the range of organisation_ informations to return (not inclusive)
	 * @return the range of organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public List<Organisation_Information> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the organisation_ informations.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of organisation_ informations to return
	 * @param end the upper bound of the range of organisation_ informations to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public List<Organisation_Information> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<Organisation_Information> list = (List<Organisation_Information>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_ORGANISATION_INFORMATION);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_ORGANISATION_INFORMATION.concat(Organisation_InformationModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<Organisation_Information>)QueryUtil.list(q,
							getDialect(), start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<Organisation_Information>)QueryUtil.list(q,
							getDialect(), start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the organisation_ informations where userId = &#63; from the database.
	 *
	 * @param userId the user id to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByUserId(String userId) throws SystemException {
		for (Organisation_Information organisation_Information : findByUserId(
				userId)) {
			remove(organisation_Information);
		}
	}

	/**
	 * Removes all the organisation_ informations from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (Organisation_Information organisation_Information : findAll()) {
			remove(organisation_Information);
		}
	}

	/**
	 * Counts all the organisation_ informations where userId = &#63;.
	 *
	 * @param userId the user id to search with
	 * @return the number of matching organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public int countByUserId(String userId) throws SystemException {
		Object[] finderArgs = new Object[] { userId };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_ORGANISATION_INFORMATION_WHERE);

			if (userId == null) {
				query.append(_FINDER_COLUMN_USERID_USERID_1);
			}
			else {
				if (userId.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_USERID_USERID_3);
				}
				else {
					query.append(_FINDER_COLUMN_USERID_USERID_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (userId != null) {
					qPos.add(userId);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the organisation_ informations.
	 *
	 * @return the number of organisation_ informations
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_ORGANISATION_INFORMATION);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the organisation_ information persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.crossriver.crgis.database.model.Organisation_Information")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<Organisation_Information>> listenersList = new ArrayList<ModelListener<Organisation_Information>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<Organisation_Information>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(Organisation_InformationImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = Application_C_of_OPersistence.class)
	protected Application_C_of_OPersistence application_C_of_OPersistence;
	@BeanReference(type = Supporting_DocumentPersistence.class)
	protected Supporting_DocumentPersistence supporting_DocumentPersistence;
	@BeanReference(type = DocumentsPersistence.class)
	protected DocumentsPersistence documentsPersistence;
	@BeanReference(type = Individual_InformationPersistence.class)
	protected Individual_InformationPersistence individual_InformationPersistence;
	@BeanReference(type = Organisation_InformationPersistence.class)
	protected Organisation_InformationPersistence organisation_InformationPersistence;
	@BeanReference(type = Contact_InformationPersistence.class)
	protected Contact_InformationPersistence contact_InformationPersistence;
	@BeanReference(type = Company_Contact_PersonPersistence.class)
	protected Company_Contact_PersonPersistence company_Contact_PersonPersistence;
	@BeanReference(type = Property_InformationPersistence.class)
	protected Property_InformationPersistence property_InformationPersistence;
	@BeanReference(type = Held_PlotsPersistence.class)
	protected Held_PlotsPersistence held_PlotsPersistence;
	@BeanReference(type = History_Of_InformationPersistence.class)
	protected History_Of_InformationPersistence history_Of_InformationPersistence;
	@BeanReference(type = TransactionsPersistence.class)
	protected TransactionsPersistence transactionsPersistence;
	@BeanReference(type = Bank_Payment_SlipPersistence.class)
	protected Bank_Payment_SlipPersistence bank_Payment_SlipPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_ORGANISATION_INFORMATION = "SELECT organisation_Information FROM Organisation_Information organisation_Information";
	private static final String _SQL_SELECT_ORGANISATION_INFORMATION_WHERE = "SELECT organisation_Information FROM Organisation_Information organisation_Information WHERE ";
	private static final String _SQL_COUNT_ORGANISATION_INFORMATION = "SELECT COUNT(organisation_Information) FROM Organisation_Information organisation_Information";
	private static final String _SQL_COUNT_ORGANISATION_INFORMATION_WHERE = "SELECT COUNT(organisation_Information) FROM Organisation_Information organisation_Information WHERE ";
	private static final String _FINDER_COLUMN_USERID_USERID_1 = "organisation_Information.userId IS NULL";
	private static final String _FINDER_COLUMN_USERID_USERID_2 = "organisation_Information.userId = ?";
	private static final String _FINDER_COLUMN_USERID_USERID_3 = "(organisation_Information.userId IS NULL OR organisation_Information.userId = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "organisation_Information.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Organisation_Information exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Organisation_Information exists with the key {";
	private static Log _log = LogFactoryUtil.getLog(Organisation_InformationPersistenceImpl.class);
}